﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataModel;
using SchoolBusiness;

namespace SchoolAdministration
{
    public partial class StaffSalary : System.Web.UI.Page
    {
        private readonly BlStaff _blstaff = new BlStaff();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DDLStaff.Enabled = false;
                txtAbsent.Enabled = false;
                txtPresentDays.Enabled = false;
                lblsuccess.Visible = false;
                LoadComboBox();
                txtdate.Text = DateTime.Now.ToShortDateString();
                Getdays();

            }
        }

        public void Getdays()
        {
            int year = Convert.ToInt32(ddlYear.Text);
            int month = int.Parse(ddlmonth.SelectedValue.ToString());
            // DateTime days=Convert.ToDateTime(year+"-"+month+"-"+0);
            int Days = DateTime.DaysInMonth(year, month);
            txtTotal.Text = Days.ToString();
        }

        private void LoadComboBox()
        {
            List<Staff> Lstaff = new List<Staff>();
            Lstaff = _blstaff.GetAllStaff();
            DDLStaff.Items.Clear();
            DDLStaff.Items.Insert(0, new ListItem("--Staff--", "0"));
            DDLStaff.DataSource = Lstaff;
            DDLStaff.DataTextField = "FirstName";
            DDLStaff.DataValueField = "StaffId";
            DDLStaff.DataBind();

            //
            ddlYear.Items.Insert(0, new ListItem(DateTime.Now.AddYears(-1).ToString("yyyy"), DateTime.Now.AddYears(-1).ToString("yyyy")));
            ddlYear.Items.Insert(1, new ListItem(DateTime.Now.Year.ToString(), DateTime.Now.Year.ToString()));
            ddlYear.Items.Insert(2, new ListItem(DateTime.Now.AddYears(1).ToString("yyyy"), DateTime.Now.AddYears(1).ToString("yyyy")));

            DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);

            for (int i = 1; i < 13; i++)
            {
                ddlmonth.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));

            }

        }

        protected void DDLStaff_SelectedIndexChanged(object sender, EventArgs e)
        {
            int month = int.Parse(ddlmonth.SelectedValue.ToString());
            int year = int.Parse(ddlYear.Text);
            int staffId = int.Parse(DDLStaff.SelectedValue.ToString());
            txtPresentDays.Text = _blstaff.GetPresentDays(staffId, month, year).ToString();
            txtAbsent.Text = _blstaff.GetAbsentDays(staffId, month, year).ToString();
            var loan = CheckLoan();
            if (loan != null)
            {
                ploan.Visible = true;
                txtLoan.Text = "NRP "+loan.LoanAmount.ToString();
                txtLoan.Enabled = false;
            }
            else
                ploan.Visible = false;
        }
        public decimal calculateDeductedAmount()
        {
            int absentDays = int.Parse(txtAbsent.Text);
            int presentDays = int.Parse(txtPresentDays.Text);
            int days=absentDays+presentDays;
            if (absentDays > 2)
            {
                decimal monthsalary = decimal.Parse(TxtAmount.Text);
                decimal deducted = (monthsalary * absentDays) / days;
                return deducted;
            }
            else if (presentDays == 0)
            {
                decimal monthsalary = decimal.Parse(TxtAmount.Text);
                decimal deducted = monthsalary;
                return deducted;
            }
            else
                return 0;

        
        }

        public Loan CheckLoan()
        {
            int sid=int.Parse(DDLStaff.SelectedValue.ToString());
            Loan loan = _blstaff.GetLoanBystaff(sid);
            return loan;
        }

        protected void ddlmonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLStaff.Enabled = true;
            Getdays();
            
        }
        public string GetDate()
        {
            string year = ddlYear.Text;
            int month = int.Parse(ddlmonth.SelectedValue.ToString());
            string date =  month + "-" + year;
            return date;
        
        }

        protected void BtnLoan_Click(object sender, EventArgs e)
        {
            StaffSal _staffsal = new StaffSal();
            _staffsal.StaffId =int.Parse(DDLStaff.SelectedValue.ToString());
            _staffsal.SalYear =Convert.ToInt32(ddlYear.Text);
            _staffsal.SalMonth = int.Parse(ddlmonth.SelectedValue.ToString());
            _staffsal.SatffSalary = Convert.ToDecimal(TxtAmount.Text);
            _staffsal.DeductedAmount = calculateDeductedAmount();
            _staffsal.Bonus = txtBonusamount.Text==null ? 0 : Convert.ToDecimal(txtBonusamount.Text);
            _staffsal.TotalAmount = _staffsal.SatffSalary - _staffsal.DeductedAmount + _staffsal.Bonus;
            _staffsal.Date = DateTime.Now;
            int result = _blstaff.SaveSalary(_staffsal);
            if (result > 0)
            {
                lblsuccess.Text = "successfully Entered";
                lblsuccess.Visible = true;
                ClearAll();

            }
        }

        private void ClearAll()
        {
            txtAbsent.Text = string.Empty;
            txtPresentDays.Text = string.Empty;
            txtBonusamount.Text = string.Empty;
            TxtAmount.Text = string.Empty;
            ddlmonth.SelectedIndex = 0;
            DDLStaff.SelectedIndex = 0;
            ddlYear.SelectedIndex = 0;
            txtTotal.Text = string.Empty;
        
        }
        protected void btnView_Click(object sender, EventArgs e)
        {
           
        }

        protected void chkloan_CheckedChanged(object sender, EventArgs e)
        {
            if (chkloan.Checked == true)
            {
                txtLoanAmount.Enabled = true;
            }
            else
                txtLoanAmount.Enabled = false;
        }

    }
}